package com.china08.yxyapi.api.comm;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.yxy.YxyQuesKnow;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyQuesKnowRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyKnowController extends YxyApiBaseController {

    @Autowired
    private YxyQuesKnowRepository yqkr;

    @ApiOperation(value = "获得知识点")
    @RequestMapping(value = "/api_yw/comm/yxyknow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> find(@RequestBody ReqModel reqModel) {
        List<RespModel> list = new ArrayList<YxyKnowController.RespModel>();
        if (StringUtils.equalsIgnoreCase(reqModel.stage, "02")
                && StringUtils.equalsIgnoreCase(reqModel.subject, "SUB53")) {
            reqModel.subject = "jcsub08";
        }
        List<YxyQuesKnow> knows = yqkr.findByStageAndSubjectAndDeleteFlagOrderByOrderNumAsc(reqModel.stage,
                reqModel.subject, "1");
        for (YxyQuesKnow yxyQuesKnow : knows) {
            RespModel model = new RespModel();
            model.id = yxyQuesKnow.getId();
            model.pId = yxyQuesKnow.getParentId();
            model.name = yxyQuesKnow.getName();
            list.add(model);
        }
        return list;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String stage;
        public String subject;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(stage, null), "请填写学段");
            Assert.notNull(StringUtils.defaultIfBlank(subject, null), "请填写学科");
        }
    }

    protected static class RespModel {
        public Integer id;
        public Integer pId;
        public String name;

    }
}
